//
//  NSMutableAttributedString+Extension.swift
//  JuMingNet
//
//  Created by AlbertYuan on 2022/3/30.
//  Copyright © 2022 wangjiayu. All rights reserved.
//

import Foundation
import UIKit

extension NSMutableAttributedString{

    /// 改版属性字符串局部颜色或字体
    /// - Parameters:
    ///   - oringString: 原始字符
    ///   - changeInfo: 需要改变的部分
    ///   - color: 颜色
    ///   - changefont: 字体
    /// - Returns: 修改后的值
    static func settingAttributeColorAndFontForSubString(oringString:String,
                                                         changeInfo:String,
                                                         color:UIColor,
                                                         changefont:UIFont) -> NSMutableAttributedString{

        let attStr = NSMutableAttributedString(string: oringString)
        let attDic = [NSAttributedString.Key.foregroundColor:color,
                      NSAttributedString.Key.font:changefont]
        let changeRange = NSString.init(string: oringString).range(of: changeInfo)
        attStr.addAttributes(attDic, range: changeRange)


        return attStr
    }
}



extension NSMutableAttributedString {
    
    /// 获取范围
    func gj_allRange() -> NSRange {
        return NSMakeRange(0,length)
    }
    /// 背景色
    @discardableResult
    func gj_backgroundColor(_ color: UIColor) -> NSMutableAttributedString {
        addAttributes([.backgroundColor:color], range: gj_allRange())
        return self
    }
    /// 文字颜色
    @discardableResult
    public func gj_color(_ color: UIColor) -> NSMutableAttributedString {
        addAttributes([.foregroundColor:color], range: gj_allRange())
        return self
    }
    
    /// 文字颜色
    @discardableResult
    public func gj_color(_ color: UIColor,_ range: NSRange) -> NSMutableAttributedString {
        addAttributes([.foregroundColor:color], range: range)
        return self
    }
    
    /// 字体
    @discardableResult
    func gj_font(_ font: UIFont) -> NSMutableAttributedString{
        addAttributes([.font:font], range: gj_allRange())
        return self
    }
    
    /// 字体
    @discardableResult
    func gj_font(_ font: UIFont,_ range: NSRange) -> NSMutableAttributedString{
           addAttributes([.font:font], range: range)
           return self
    }
    
    /// 系统字体大小
    @discardableResult
    func gj_fontSize(_ size: CGFloat)->NSMutableAttributedString{
        addAttributes([.font:UIFont.systemFont(ofSize: size)], range: gj_allRange())
        return self
    }
    
    /// 添加行间距
    @discardableResult
    func gj_addLineSpace(_ space: CGFloat) -> NSMutableAttributedString {
        let style = NSMutableParagraphStyle()
        style.lineSpacing = space
        style.lineBreakMode = .byCharWrapping
        addAttribute(.paragraphStyle, value: style, range: gj_allRange())
        return self
    }
    /// 拼接富文本
    @discardableResult
    func gj_addAttribute(_ attribute: NSMutableAttributedString) -> NSMutableAttributedString {
        append(attribute)
        return self
    }
}
